/**
 * 计算日期天数
 */
public class CalDays {

    private String date;

    private String[] dateArray;

    private int year;

    private int month;

    private int day;

    public String[] getDateArray() {
        return dateArray;
    }

    public void setDateArray(String[] dateArray) {
        this.dateArray = dateArray;
    }


    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    /**
     * 构造函数
     *
     * @param date
     */
    public CalDays(String date) throws Exception {
        this.date = date;
        this.dateArray = date.trim().split("-");

        if (dateArray.length != 3) {
            throw new Exception("输入日期有误");
        }

        this.year = Integer.parseInt(dateArray[0]);
        this.month = Integer.parseInt(dateArray[1]);
        this.day = Integer.parseInt(dateArray[2]);
    }

    /**
     * 是否闰年
     *
     * @param year
     * @return
     */
    public boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    /**
     * 月份天数
     *
     * @param month
     * @return
     */
    public int monthDays(int year, int month) {
        int days = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if (isLeapYear(year)) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
        }
        return days;
    }

    /**
     * 计算天数
     *
     * @return
     */
    public String calculationDays() {
        int sum = 1;
        for (int i = 1; i < this.month; i++) {
//            System.out.println(monthDays(this.year, i));
            sum += monthDays(this.year, i);
        }
        return month + "月" + day + "日在" + year + "年中是第" + sum + "天";
    }
}
